import type {IResponseBase, IResponsePage} from '@/typings/api';

export const baseUrl = 'http://192.168.3.51:8081/api';

type ToReturnRes<T, P extends boolean> = P extends true
    ? IResponsePage<T>
    : IResponseBase<T>;

export interface UniappResponse<T, P extends boolean> {
    statusCode: number;
    errMsg: string;
    header: {
        'content-type': string;
    };
    data: ToReturnRes<T, P>;
}

/**
 * 模拟响应拦截器
 */
function interceptorsResponse<T, P extends boolean>(res: UniappResponse<T, P>) {
    const data = res.data as ToReturnRes<T, P>;

    return data;
}

/**
 * 模拟请求拦截器
 */
function interceptorsRequest(req?: Omit<UniNamespace.RequestOptions, 'url'>) {
    req = req || {};
    const {token} = $(useUser());
    if (token) {
        req.header = req.header || {};
        req.header.authorization = token;
    }

    return req;
}

export default async function request<T = null, P extends boolean = false>(
    url: string,
    req?: Omit<UniNamespace.RequestOptions, 'url'>
) {
    return (await uni
        .request({url: `${baseUrl}${url}`, ...interceptorsRequest(req)})
        .then(
            // @ts-expect-error-error
            (res) => interceptorsResponse(res)
        )) as ToReturnRes<T, P>;
}

